﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace OINS_VS08
{
    class Resources
    {
        //  Vector containing the list of files, we share.
        List<Record> ListOfFiles=new List<Record>();

       
        //Method to update list of files by adding new folder
        public void AddFolderToResources(String Director)
        {
            DirectoryInfo dir = new DirectoryInfo(Director);
            DirectoryInfo[] directories = dir.GetDirectories();
            if (directories != null)
            {
                foreach (DirectoryInfo newDir in directories)
                {
                    
                    String a=newDir.Attributes.ToString();
                    
                    if ( a.Contains("System") )
                       continue;
                    FileInfo[] filesInDir;
                    try
                    {
                        filesInDir = newDir.GetFiles();
                    }
                    catch (UnauthorizedAccessException)           //wyjatek do braku uprawnien, ogolnie troche lipka, bo wypierdala caly katalog, chociaz tylko do kilku nie mam praw, co i tak jest dziwne, bo systemowych nie przegladam kurwa, sprawdz sobie wpisujac c:/ sciezke
                    {
                        filesInDir = null;
                    }
                    if (filesInDir == null)
                        continue;
                    foreach (FileInfo file in filesInDir)
                    {
                        ListOfFiles.Add(new Record(file.Name, file.Extension));
                       
                        //StreamWriter stream = new StreamWriter("d:/list.txt",true);
                        //stream.WriteLine(file.Name + "\t" + file.Extension);
                       // stream.Close(); ;
                    }
                    Console.WriteLine(newDir.Name);

                    AddFolderToResources(Path.Combine(Director,newDir.Name));

                    

                }
            }
            //SaveTheList();
            
        }
       /* public void SearchForFiles(String argument)
        {
            foreach(Record ListOfFiles 

        }*/

        public void ShowList()
        {
            foreach(Record wpis in ListOfFiles)
            {
                Console.WriteLine(" Nazwa :{0} Typ: {1}",wpis.fileName,wpis.extension);
            }
        }
        public void SaveTheList()
        {
            FileInfo saveTheList = new FileInfo("d:/list.txt");
           

                StreamWriter stream = new StreamWriter("d:/list.txt",false);
                foreach (Record wpis in ListOfFiles)
                    stream.WriteLine(wpis.fileName + "\t" + wpis.extension);
                stream.Close(); ;

            
        }
        public void LoadTheList()
        {
            FileInfo list = new FileInfo("d:/list.txt");
            StreamReader stream = list.OpenText();
            String line;
            do
            {
                line = stream.ReadLine();
                if (line != null)
                {
                    String[] temp = line.Split('\t');
                    ListOfFiles.Add(new Record(temp[0], temp[1]));
                }
                else
                    continue;
            } while (line != null);
            stream.Close();
        }
    }
}
